package com.shujia.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Demo2FastJson {
    public static void main(String[] args) {

        String jsonstr = "{\"id\":\"1500100001\",\"age\":22,\"gender\":\"女\",\"clazz\":\"文科六班\"}";

        //将json字符串转换成json对象，json对象可以直接使用列名取值
        JSONObject jsonObject = JSON.parseObject(jsonstr);

        System.out.println(jsonObject.get("id"));
        System.out.println(jsonObject.getString("gender"));
        System.out.println(jsonObject.getInteger("age"));
        System.out.println(jsonObject.getString("clazz"));


        //将json字符串转换成自定义类的对象
        Student student = JSON.parseObject(jsonstr, Student.class);
        System.out.println(student);


        //将对象转换成json字符串
        String s = JSON.toJSONString(student);

        System.out.println(s);


    }
}
